package com.iotimc.attendancesystem.manage;


import android.app.smdt.SmdtManager;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.base.BaseActivity;
import com.iotimc.attendancesystem.dao.GreenDaoManager;
import com.iotimc.attendancesystem.dao.PersonEntityDao;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.iotimc.attendancesystem.manage.fragment.AttendanceLogFragment;
import com.iotimc.attendancesystem.manage.fragment.DoorLogFragment;
import com.iotimc.attendancesystem.manage.fragment.SystemSettingFragment;
import com.iotimc.attendancesystem.manage.person.PersonManageFragment;
import com.iotimc.attendancesystem.util.PictureManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by cwj on 2017/11/30.
 */

public class ManageActivity extends BaseActivity {
    @BindView(R.id.ll_manage_home)
    LinearLayout mLlManageHome;
    @BindView(R.id.fl_manage_content)
    FrameLayout mFlManageContent;
    @BindView(R.id.iv_manage_admin_img)
    ImageView ivManageAdminImg;

    //需要监听几次点击事件数组的长度就为几
    //如果要监听双击事件则数组长度为2，如果要监听3次连续点击事件则数组长度为3...
    long[] mHints = new long[5];//初始全部为0

    @OnClick(R.id.iv_manage_admin_img)
    void onBack() {
        //将mHints数组内的所有元素左移一个位置
        System.arraycopy(mHints, 1, mHints, 0, mHints.length - 1);
        //获得当前系统已经启动的时间
        mHints[mHints.length - 1] = SystemClock.uptimeMillis();
        if (SystemClock.uptimeMillis() - mHints[0] <= 2000) {
            SmdtManager.create(this).smdtSetStatusBar(this,true);
        }
    }

    @BindView(R.id.tv_manage_admin_name)
    TextView tvManageAdminName;
    @BindView(R.id.iv_manage_user)
    ImageView ivManageUser;
    @BindView(R.id.fl_manage_user)
    FrameLayout flManageUser;
    @BindView(R.id.iv_manage_work)
    ImageView ivManageWork;
    @BindView(R.id.fl_manage_work)
    FrameLayout flManageWork;
    @BindView(R.id.iv_manage_door)
    ImageView ivManageDoor;
    @BindView(R.id.fl_manage_door)
    FrameLayout flManageDoor;
    @BindView(R.id.iv_manage_setting)
    ImageView ivManageSetting;
    @BindView(R.id.fl_manage_setting)
    FrameLayout flManageSetting;
    @BindView(R.id.tv_manage_tittle)
    TextView tvManageTittle;
    private ImageView select;
    private PersonEntityDao personEntityDao;
    private Fragment currentFragment;

    @OnClick(R.id.ll_manage_home)
    void goHome() {
        int requestCode = 9527;
        setResult(requestCode);
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.exit_scale);
    }

    @OnClick(R.id.fl_manage_user)
    void goUser() {
        if (!ivManageUser.equals(select)) {
            setSelect(ivManageUser);
            tvManageTittle.setText("用户管理");
            switchContent(personManageFragment);
        }

    }

    DoorLogFragment doorLogFragment;

    @OnClick(R.id.fl_manage_door)
    void goDoor() {
        if (!ivManageDoor.equals(select)) {
            setSelect(ivManageDoor);
            tvManageTittle.setText("开关门记录");
            if (doorLogFragment == null) {
                doorLogFragment = new DoorLogFragment();
            }
            switchContent(doorLogFragment);
        }

    }

    AttendanceLogFragment attendanceLogFragment;

    @OnClick(R.id.fl_manage_work)
    void goSetting() {
        if (!ivManageWork.equals(select)) {
            setSelect(ivManageWork);
            tvManageTittle.setText("考勤记录");
            if (attendanceLogFragment == null) {
                attendanceLogFragment = new AttendanceLogFragment();
            }
            switchContent(attendanceLogFragment);

        }

    }

    SystemSettingFragment systemSettingFragment;

    @OnClick(R.id.fl_manage_setting)
    void goWork() {
        if (!ivManageSetting.equals(select)) {
            setSelect(ivManageSetting);
            tvManageTittle.setText("系统设置");
            if (systemSettingFragment == null) {
                systemSettingFragment = new SystemSettingFragment();
            }
            switchContent(systemSettingFragment);


        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (ManageActivity.this.getCurrentFocus() != null) {
                if (ManageActivity.this.getCurrentFocus().getWindowToken() != null) {
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(ManageActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected int getResId() {
        return R.layout.activity_manage;
    }

    PersonManageFragment personManageFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        this.select = ivManageUser;
        personManageFragment = new PersonManageFragment();
        currentFragment = personManageFragment;
        switchContent(personManageFragment);
        Bundle extras = getIntent().getExtras();
        boolean admin = false;
        String code = null;
        if (extras != null) {
            admin = extras.getBoolean("admin", false);
            code = extras.getString("code", "");
        }

        if (admin && code != null) {
            personEntityDao = GreenDaoManager.getInstance().getSession().getPersonEntityDao();

            //获取管理员
            PersonEntity entity = personEntityDao.queryBuilder().where(PersonEntityDao.Properties.Admin.eq(true), PersonEntityDao.Properties.Code.eq(code)).unique();

            if (entity != null) {
                String name = "您好!" + entity.getName();
                tvManageAdminName.setText(name);
                byte[] image = entity.getImage();
                PictureManager.getInstance().loadCirclePic(ManageActivity.this, image, ivManageAdminImg, false);
            } else {
                String name = "您好!管理员";
                tvManageAdminName.setText(name);
                PictureManager.getInstance().loadCirclePic(ManageActivity.this, R.drawable.ic_user_img, ivManageAdminImg, false);
            }
        } else {
            String name = "您好!管理员";
            tvManageAdminName.setText(name);
            PictureManager.getInstance().loadCirclePic(ManageActivity.this, R.drawable.ic_user_img, ivManageAdminImg, false);
        }


    }


    private void setSelect(ImageView select) {
        if (this.select != null) {
            this.select.setVisibility(View.GONE);
        }
        select.setVisibility(View.VISIBLE);
        this.select = select;
    }

    public void switchContent(Fragment to) {
        if (!to.isAdded()) {
            if (currentFragment != to) {
                getSupportFragmentManager().beginTransaction().hide(currentFragment).commit();
            }
            getSupportFragmentManager().beginTransaction()/*.setCustomAnimations(0,R.anim.exit_scale)*/.add(R.id.fl_manage_content, to).commit();
        } else {
            getSupportFragmentManager().beginTransaction()/*.setCustomAnimations(R.anim.exit_scale,0)*/.hide(currentFragment).show(to).commit();
        }
        currentFragment = to;
    }

}
